from datetime import timedelta
from rest_framework.views import APIView
from certificate_manager.minio import MinioSingleton
from rest_framework.response import Response
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from geexin_server.settings import MINIO_SERVER_ACCESS_KEY, MINIO_SERVER_SECRET_KEY, MINIO_SERVER_HOST, MINIO_SERVER_BUCKET

class MinioSupport(APIView):
    authentication_classes = [TokenAuthentication]
    permission_classes = [IsAuthenticated]
    
    def get(self, request, format=None, name=None):
        filename = request.query_params['name']
        minio_client = MinioSingleton.getMinioServer()
        assert(minio_client != None)
        # 这里按理说应该指定文件名的 不安全啊
        back_url = minio_client.presigned_put_object(MINIO_SERVER_BUCKET, filename, timedelta(seconds=1 * 60 * 60)) # 1h
        back_url = back_url.replace('http://localhost:9000', '/oss/')
        # 这里需要指定可以访问的地址
        return Response({'url': back_url})
